package jm.data.structure.set;

import jm.data.structure.tree.BinaryTree;
import jm.data.structure.tree.RBTree;

/**
 * @Description 红黑树实现set
 * @date 2022/4/23 15:04
 */
public class TreeSet<E> implements Set<E>{

    private RBTree<E> tree = new RBTree<>();

    @Override
    public int size() {
        return tree.size();
    }

    @Override
    public boolean isEmpty() {
        return tree.isEmpty();
    }

    @Override
    public void clear() {
        tree.clear();
    }

    @Override
    public boolean contains(E e) {
        return tree.contains(e);
    }

    @Override
    public void add(E e) {
        // 红黑树中已经实现了，如果相同就覆盖的功能。
        tree.add(e);
    }

    @Override
    public void remove(E e) {
        tree.remove(e);
    }

    @Override
    public void traversal(Visitor<E> visitor) {
        // 使用中序遍历
        // 调用二叉树的visitor
        tree.inorderTraversal(new BinaryTree.Visitor<E>() {
            @Override
            public boolean visit(E e) {
                return visitor.visit(e);
            }
        });
    }
}
